package com.tracy.lifeproject.discovery.goods.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tracy.lifeproject.R;
import com.tracy.lifeproject.bean.GoodsBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.discovery.goods.activity.GoodsItemActivity;
import com.tracy.lifeproject.discovery.goods.adapter.GoodsRecyclerAdapter;
import com.tracy.lifeproject.discovery.goods.presenter.GoodsPresenter;
import com.tracy.lifeproject.discovery.goods.view.GoodsView;

import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsFragment extends Fragment implements GoodsView {

    private Activity mActivity;
    private RecyclerView recyclerView;
    private GoodsPresenter mPresenter;
    private GoodsRecyclerAdapter mAdapter;
    private String url = UrlString.URL_GOODS;
    private PtrClassicFrameLayout ptr_goods;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mPresenter = new GoodsPresenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_goods, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_goods);

        ptr_goods = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_goods);

//        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));

        mAdapter = new GoodsRecyclerAdapter(mActivity);
        recyclerView.setAdapter(mAdapter);
        mPresenter.onLoadDatas(url);

        ptr_goods.setResistance(1.8f);//下拉阻力
        ptr_goods.setRatioOfHeaderHeightToRefresh(1.2f);//1.2倍头部高度刷新
        ptr_goods.setDurationToClose(200);//回弹到刷新高度的时间
        ptr_goods.setDurationToCloseHeader(1000);//刷新完成隐藏的时间
        ptr_goods.setPullToRefresh(false);//释放时刷新
        ptr_goods.setKeepHeaderWhenRefresh(true);//刷新时保持头部

        ptr_goods.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPresenter.onLoadDatas(url);
                ptr_goods.refreshComplete();
//                Log.i("zjs","1234567899876543212345678");
            }
        });
    }

    @Override
    public void onFailure() {
        Toast.makeText(mActivity, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loadDatas(List<GoodsBean.DataBean.CategoriesBean> datas) {
        mAdapter.setDatas(datas);
        Log.i("zjs","loadsuccess");
        mAdapter.setOnKeyWordViewItemClickListencer(new GoodsRecyclerAdapter.OnKeyWordViewItemClickListencer() {
            @Override
            public void onItemClickd(String url,String itemName, View view) {
//                Toast.makeText(mActivity, url, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mActivity, GoodsItemActivity.class);
                intent.putExtra("url",url);
                intent.putExtra("itemName",itemName);
                startActivity(intent);
            }
        });
    }
}
